package moodish.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * This class represents the North panel that is added to the ClientGUI.
 * This panel appears in the top and shows the banner and the connection status button.
 *  
 * @author es1-2013-group2
 *
 */
public class NorthPanel extends JPanel{

	private JButton button;
	private ImageIcon circleRed;
	private ImageIcon circleGreen;
	private ImageIcon title;
	private ClientGUI clientGUI;
	

	/**
	 * 
	 * The constructor creates a new NorthPanel with the parameter ClientGUI.
	 * Sets the layout of this panel.
	 * The actionListener change the color of button (red-disconnected, green-connected). 
	 * Shows a list of available servers.
	 *  
	 * @param clientGUI ClientGUI.
	 */
	public NorthPanel(final ClientGUI clientGUI){
		this.clientGUI = clientGUI;
		circleGreen = new ImageIcon(getClass().getResource("/circle_green.png"));
		circleRed = new ImageIcon(getClass().getResource("/circle_red.png"));    
		title = new ImageIcon(getClass().getResource("/banner2.png"));
		JLabel titleLabel = new JLabel(title);

		button = new JButton(circleRed);
		button.setToolTipText("Connect");
		button.setContentAreaFilled(false);
		button.setFocusPainted(false);
		button.setBorderPainted(false);
		button.setOpaque(false);
		button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
		button.setBackground(Color.BLACK);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				if(clientGUI.getUser().isConnected()){
					clientGUI.disconnect(1);
					
				}
				else{
					String[] hosts = {"localhost"};
					JFrame frame = new JFrame();

					String host = (String) JOptionPane.showInputDialog(frame, "Server: ",
							"Connect to Server",
							JOptionPane.PLAIN_MESSAGE,
							null,
							hosts,
							hosts[0]);

					clientGUI.connect(host);
					button.setToolTipText("Disconnect");
					button.setIcon(circleGreen);

				}
			}
		});

		this.setBackground(Color.BLACK);
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK, 10));
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));	
		this.add(titleLabel);
		this.add(Box.createRigidArea(new Dimension(40, 0)));
		this.add(button);
		this.setPreferredSize(new Dimension(920, 110));
	}
	
	
	/**
	 * Set the color of the connection status button to red.
	 * 
	 */
	public void setButtonRed(){
		button.setIcon(circleRed);
	}
	
	/**
	 * Get the Connect Button
	 * @return button
	 */
	public JButton getButton(){
		return button;
	}


}
